local GetTime = GetTime();
--
local start, duration = GetSpellCooldown("Rend");
local gcd = start + duration;
local function GetCooldown(id)
  local start, duration = GetSpellCooldown(id);
  local cd = start + duration - gcd;
  if cd < 0 then return 0 end
  return cd
end

-- by Lars: checks if 10% physical damage reduced debuff is on target, returns the name of the spell, time of start and duration
-- and saves some lines of code.
local physDmgReduce={"Demoralizing Roar", "Demoralizing Shout", "Curse of Weakness", "Scarlet Fever", "Vindication"};
local physDmgReduceMinDuration = 6;
local function PhysDmgReducedInfo()
  for idx=1,#physDmgReduce do
    local _, _, start, duration = IconAura("HARMFUL", "target", physDmgReduce[idx]); 
    if(duration) then 
      if (duration+start-GetTime>physDmgReduceMinDuration) then
        return false;
      end
    end
  end
  return true;
end
--
-- by Calo: Check Health buff.
local healthBuffs = {"Commanding Shout", "Blood Pact", "Power Word: Fortitude"}
local function NeedHealthBuff()
  for idx=1,#healthBuffs do
    local _, _, _, durationHB = IconAura("HELPFUL", "player", healthBuffs[idx]);
    if (durationHB) then
      return false;
    end
  end
  return true;
end
--Cases:
--1-) If I'm not on Defensive Stance, I should switch to it.
if (GetBonusBarOffset() ~= 2) then
  return IconSpell("Defensive Stance");
end
--2-) If I don't have a target or my target is not attackable / is
--dead, no need for anything else.
if (not (UnitExists("target") and UnitCanAttack("player", "target"))) or (UnitIsDeadOrGhost("target")) then
  return
end

--3-) If my Shield Block is available, use it. Not only it is 
--an extremely powerful defensive cooldown, but also it is a very 
--nice dps boost. Truth be told, I generally wait for this one if 
--I'm going to receive a big regular / spell hit. If that's not the
--case, wait for shield slam to be available.
if (GetCooldown("Shield Block") <= 0) then
  return IconSpell("Shield Block");
end

--4-) If I don't have Health Buff, I should use commanding shout.
--Checking for Commanding Shout (warriors), Blood Pact (warlock / imp),
--Power Word: Fortitude (Priest). If none of these are up, I should
--shout.
if (NeedHealthBuff()) then
  return IconSpell("Commanding Shout");
end
--5-) If my target does not have %10 physical damage reduction debuff, 
--I should apply. These are, Demoralizing Shout (Warriors), Curse of 
--Weakness (Warlocks), Demoralizing Roar (Bear), Scarlet Fever 
--(Blood Deathknights), Vindication (Prot Paladins).I also have 
--another icon, "just" to display if my target has Demoralizing Shout.
--Not checking other debuffs. Shouting when I can if that icon is up, 
--shouting immediately when this one is up.
--by Lars: 6 seconds before debuff will disappear demo-shout icon will apear
if (PhysDmgReducedInfo()) then
  return IconSpell ("Demoralizing Shout");
end
--6-) If I'm over 70 rage & inner rage is available, use it & spam
--heroic strikes. This could've belong to dps behavior, however as
--neither Inner Rage or Heroic Strike triggers global cooldown.
--Therefore, my dps rotation is not affected by neither.
if (UnitPower("player")>70 and GetCooldown("Inner Rage")<= 0) then
  return IconSpell("Inner Rage");
end
--7-) What you do if you got nothing else to do  You shout to get rage.
if (GetCooldown("Commanding Shout") <= 0) then
  return IconSpell("Commanding Shout");
end